<?php
namespace app\Admin\controller;

//引入公共控制器
use app\common\controller\Backend;

//引入数据库
use think\Db;

//引入cookie
use think\Cookie;

use think\Request;

class Order extends Backend {
    public function __construct() {

        // 继承父类
        parent::__construct();

        // 引入模型
        $this->OrderModel = model( 'Order.Order' );
        $this->AddressModel = model( 'User.Address' );
        $this->OrderProductModel = model( 'Order.Product' );
    }

    // 获取订单列表

    public function index() {
        // 获取开始时间
        $start = $this->request->param( 'start', '' );

        // 获取结束时间
        $end = $this->request->param( 'end', '' );

        // 获取关键字
        $keywords = $this->request->param( 'keywords', '' );

        // 条件集合
        $where = [];

        if ( $start ) {
            // 加入条件 大于开始时间
            $where[ 'order.createtime' ] = [ '> time', $start ];
        }

        if ( $end ) {
            // 加入条件 小于或等于结束时间
            $where[ 'order.createtime' ] = [ '<= time', $end ];
        }

        // 若两个都存在就将两个条件拼接
        if ( $start && $end ) {
            $where[ 'order.createtime' ] = [ 'between time', [ $start, $end ] ];
        }

        if ( $keywords ) {
            // 加入条件 关键字
            $where[ 'order.code' ] = [ 'like', "%$keywords%" ];
        }
        $count = $this->OrderModel->count();
        $orderlist = $this->OrderModel->with( [ 'express', 'user' ] )->where( $where )->order( 'createtime', 'desc' )->paginate( 10, false, [ 'query' => Request::instance()->param() ] );

        foreach ( $orderlist as $item ) {
            // 添加地址信息
            $item[ 'address' ] = $this->AddressModel->where( [ 'id' => $item[ 'addrid' ] ] )->find();
            $item[ 'address' ] = json_encode( $item[ 'address' ] );

            // 添加商品信息
            $item[ 'product' ] = $this->OrderProductModel->with( [ 'info' ] )->where( [ 'orderid' => $item[ 'id' ] ] )->select();
            $item[ 'product' ] = json_encode( $item[ 'product' ] );

            // 状态
        }

        $orderstr = $orderlist->render();

        $this->assign( [
            'count' => $count,
            'orderlist' => $orderlist,
            'orderstr' => $orderstr,
            'keywords' => $keywords,
            'start' => $start,
            'end' => $end,
        ] );

        return $this->fetch();
    }

    /* 删除商品 */

    public function del() {
        if ( $this->request->isPost() ) {
            // 提示信息
            $success = [
                'result' => false,
                'msg' => '',
            ];

            $ids = $this->request->param( 'ids', 0 );

            // 软删除
            $result = $this->OrderModel->destroy( $ids );

            if ( $result === FALSE ) {
                $success[ 'result' ] = false;
                $success[ 'msg' ] = $this->OrderModel->getError();
            } else {
                $success[ 'result' ] = true;
                $success[ 'msg' ] = '添加回收站成功！';
            }

            return json( $success );
        }
    }

    public function recycle() {
        // 获取开始时间
        $start = $this->request->param( 'start', '' );

        // 获取结束时间
        $end = $this->request->param( 'end', '' );

        // 获取关键字
        $keywords = $this->request->param( 'keywords', '' );

        // 条件集合
        $where = [];

        if ( $start ) {
            // 加入条件 大于开始时间
            $where[ 'order.createtime' ] = [ '> time', $start ];
        }

        if ( $end ) {
            // 加入条件 小于或等于结束时间
            $where[ 'order.createtime' ] = [ '<= time', $end ];
        }

        // 若两个都存在就将两个条件拼接
        if ( $start && $end ) {
            $where[ 'order.createtime' ] = [ 'between time', [ $start, $end ] ];
        }

        if ( $keywords ) {
            // 加入条件 关键字
            $where[ 'order.code' ] = [ 'like', "%$keywords%" ];
        }
        $count = $this->OrderModel->onlyTrashed()->count();
        $orderlist = $this->OrderModel->onlyTrashed()->with( [ 'express', 'user' ] )->where( $where )->order( 'createtime', 'desc' )->paginate( 10, false, [ 'query' => Request::instance()->param() ] );

        foreach ( $orderlist as $item ) {
            // 添加地址信息
            $item[ 'address' ] = $this->AddressModel->where( [ 'id' => $item[ 'addrid' ] ] )->find();
            $item[ 'address' ] = json_encode( $item[ 'address' ] );

            // 添加商品信息
            $item[ 'product' ] = $this->OrderProductModel->with( [ 'info' ] )->where( [ 'orderid' => $item[ 'id' ] ] )->select();
            $item[ 'product' ] = json_encode( $item[ 'product' ] );

            // 状态
        }

        $orderstr = $orderlist->render();

        $this->assign( [
            'count' => $count,
            'orderlist' => $orderlist,
            'orderstr' => $orderstr,
            'keywords' => $keywords,
            'start' => $start,
            'end' => $end,
        ] );

        return $this->fetch();
    }

    // 取消软删除 恢复正常

    public function recovery() {
        if ( $this->request->isPost() ) {
            // 提示信息
            $success = [
                'result' => false,
                'msg' => '',
            ];

            $ids = $this->request->param( 'ids', 0 );

            $where = 'id in('.$ids.')';

            $data = [
                'deletetime' => null,
            ];

            // 软删除
            $result = $this->OrderModel->onlyTrashed()->where( $where )->update( [ 'deletetime' => NULL ] );

            if ( $result === FALSE ) {
                $success[ 'result' ] = false;
                $success[ 'msg' ] = $this->ProductModel->getError();
            } else {
                $success[ 'result' ] = true;
                $success[ 'msg' ] = '恢复商品成功';
            }

            return json( $success );
        }

    }

    // 真正删除

    public function cut() {
        if ( $this->request->isPost() ) {
            // 提示信息
            $success = [
                'result' => false,
                'msg' => '',
            ];

            $ids = $this->request->param( 'ids', 0 );

            $where = 'id in('.$ids.')';

            // 软删除
            $result = $this->OrderModel->onlyTrashed()->where( $where )->delete();

            if ( $result === FALSE ) {
                $success[ 'result' ] = false;
                $success[ 'msg' ] = $this->OrderModel->getError();
            } else {
                $success[ 'result' ] = true;
                $success[ 'msg' ] = '删除商品成功';
            }

            return json( $success );
        }
        return '456';
    }

}
?>